<?php
Class V_User {
    private static $_login;
    private static $_name;
    private static $_group;
    private static $_auth;
    private static $_id;
    private static $_city;
    private static $_service;
    public static function activateModule () {
        self::$_auth=false;
  	if ((isset($_SESSION['auth']))and($_SESSION['auth']==true))  {
		$templogin=$_SESSION['login'];
		$temppass=$_SESSION['pass'];
        $tempuser=V_DB::fetch('SELECT id, pass, login, group_id, name, user_city_id, user_service_id FROM v_user_list '.
            				'WHERE login="'.$templogin.'" AND pass="'.$temppass.'"');
		if (!empty($tempuser) and sizeof($tempuser)>0) {
        		self::$_login=$tempuser['login'];
        		self::$_auth=true;
         		self::$_group=$tempuser['group_id'];
        		self::$_id=$tempuser['id'];
        		self::$_name=$tempuser['name'];
        		self::$_city=$tempuser['user_city_id'];
        		self::$_service=$tempuser['user_service_id'];
       			unset ($templogin);
       			unset ($temppass);
        		unset ($tempuser);
  			}
 	 	}
        if (!self::$_auth) {
             self::$_login='guest';
             self::$_auth=false;
             self::$_group=-1;
             self::$_id=-1;
             self::$_name='Guest';
         }
    }
    public static function getGroup () {
        return self::$_group;
    }
    public static function getName () {
        return self::$_name;
    }
	public static function getLogin () {
        return self::$_login;
    }
    public static function getId () {
        return self::$_id;
    }
	public static function getCity () {
        return self::$_city;
    }
	public static function getService () {
        return self::$_service;
    }
    public static function isAuth () {
        return self::$_auth;
    }
    // Проверка роли пользователя
    public static function checkRoles ($resource) {
        $find=false;
        if ($res = V_Config::getValue('roles',self::$_group)) {
            $roles = explode ('-',$resource);
            $i=0;
            $allow=false;
            while (isset($roles[$i]) and isset($res[$roles[$i]])) {
                if ($res[$roles[$i]] == 'allow') {
                    $allow = true;
                    $find = true;
                }
                elseif ($res[$roles[$i]] == 'deny') {
                    $allow = false;
                    $find = true;
                }
                else $res = $res[$roles[$i]];
                $i++;
            }
        }
        if (!$find and ($rule = V_Config::getValue('system','role'))) {
            if ($rule == 'allow') $allow = true;
            elseif ($rule == 'deny') $allow = false;
        }
        return $allow;
    }
    // Получить страницу по умолчанию для группы
    public static function getDefaultPage () {
        if ($page=V_Config::getValue('roles',self::$_group))
            return $page['default'];
        else return 'auth';
    }
}